package cn.pzhuWEB.llk.controller;

import cn.pzhuWEB.llk.entity.*;
import cn.pzhuWEB.llk.service.CardService;
import cn.pzhuWEB.llk.service.PuzzleInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import cn.pzhuWEB.llk.entity.Grade;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpSession;

import java.util.ArrayList;
import java.util.List;


@RestController
@RequestMapping(value = "/puzzle")
@CrossOrigin

@Controller
public class PuzzleController {
    @Autowired
    CardService cardService;
    @Autowired
    PuzzleInfoService puzzleInfoService;
    /**
     * 获得一张图片
     * @return
     */
//    @GetMapping("/{partten}/{level}")
//    public String getOnePhoto(@PathVariable int partten, @PathVariable int level, HttpSession session){
//        /**
//         * 0 1 拖拽模式、点击模式
//         * 1、2、3 简单、中级、高级
//         *
//         * 更新01游戏排行榜
//         * 更新02游戏排行榜
//         * 更新03游戏排行榜
//         *
//         * 更新11游戏排行榜
//         * 更新12游戏排行榜
//         * 更新13游戏排行榜
//         */
//        String patt;
//        switch (partten){
//            case 0:patt="拖拽模式";
//            case 1:patt="点击模式";
//            default:patt=null;
//        }
    @GetMapping("/{pattern}/{level}")
    public Result getOnePhoto(@PathVariable int pattern, @PathVariable int level, HttpSession session){
        //查询所有图片
        ArrayList<ImagePath> allPhoto = puzzleInfoService.findAllPhoto();
        //随机获得一张
        int size = allPhoto.size();
        int i = (int) (Math.random() * size);
        ImagePath imagePath = allPhoto.get(i);
        //向session保存信息
        session.setAttribute("partten",pattern);
        session.setAttribute("level",level);
        return new Result(1,imagePath);
    }

    /**
     * 用户完成游戏
     */
    @PostMapping("/{pattern}/{level}/{completeTime}")
    public Grade complete(@PathVariable int pattern ,@PathVariable int level,@PathVariable String completeTime, HttpSession session) {
        //获取用户信息
        User user = (User) session.getAttribute("user");
        String stuCard = user.getStuCard();
        String partt;
        switch (pattern){
            case 0:partt="拖拽模式";break;
            case 1:partt="点击模式";break;
            default:partt=null;
        }
        //保存用户该游戏的日志
        long l = Long.parseLong(completeTime);
        String formatTime = format(l);
        PuzzleRecord puzzleRecord = new PuzzleRecord(user.getStuCard(), null, formatTime, partt, level);
        //加入做题日志
        puzzleInfoService.insertPuzzleGrade(puzzleRecord);
        String compTime = puzzleInfoService.queryCompTime(puzzleRecord);
        if (compTime == null || compTime.compareTo(formatTime) > 0) {
            if (compTime == null) {
                puzzleInfoService.insertPuzzleRank(puzzleRecord);
            } else {
                puzzleInfoService.updatePuzzleRank(puzzleRecord);
            }
            Integer gradeCount = puzzleInfoService.findGradeByTimeRank(puzzleRecord);
            return new Grade(compTime,gradeCount);
        } else {
            Integer gradeCount = puzzleInfoService.findGradeByTimeRank(puzzleRecord);
            //您本次成绩****，个人最好成绩为：****，个人最高排名：***，再接再厉哦！
            return new Grade(compTime,gradeCount);
        }
    }
    /**
     * 查询拼图游戏用户排行榜
     * @return
     */
    @GetMapping("/grade/{pattern}/{level}")
    public List<User> getGrade(@PathVariable int pattern, @PathVariable int level){
        //通过模式和等级关键字，查询该游戏排名
        String  partt="";
        switch (pattern){
            case 0:partt="拖拽模式";break;
            case 1:partt="点击模式";break;
            default:partt=null;
        }
        List<User> gradeByPatternAndLevel = puzzleInfoService.findGradeByPatternAndLevel(partt, level);
        return gradeByPatternAndLevel;
    }

    private String format(long elapsed) {
        int minute, second, milli;
        milli = (int) (elapsed % 1000);
        elapsed = elapsed / 1000;
        second = (int) (elapsed % 60);
        elapsed = elapsed / 60;
        minute = (int) (elapsed % 60);
        return String.format("%02d:%02d %03d", minute, second, milli);
    }
}
